<?php
/**
 * 为主要模板添加一套class
 *
 * 大多数功能参照的Sandbox theme
 * @链接 http://www.plaintxt.org/themes/sandbox
 *
 * one_entry_class()
 * one_comment_class()
 * one_body_class()
 *
 * @package one
 * @subpackage Functions
 */

/**
 * 为全站的entry添加一套class
 *
 * @link http://codex.wordpress.org/Conditional_Tags
 * @link http://codex.wordpress.org/Template_Tags/get_the_category
 * @link http://codex.wordpress.org/Template_Tags/get_the_tags
 *
 * @since 1.0.0
 * @参数 string|array $class
 * @return string
 */
function one_entry_class( $class = '' ) {
	global $post;
	static $post_alt;

	/*
	* Microformats compliance
	*/
	$classes[] = 'hentry';

	/*
	* Post type (page, post, etc.)
	* For now (to assure backwards compatibility w/stylesheets),
	* All entries should be given a class of 'post'
	*/
	$classes[] = $post->post_type;

	if ( $post->post_type != 'post' )
		$classes[] = 'post';

	/*
	* Post alt class
	*/
	++$post_alt;
	$classes[] = 'post-' . $post_alt;

	if ( $post_alt % 2 )
		$classes[] = 'even alt';
	else
		$classes[] = 'odd';

	/*
	* Sticky class (only on home/blog page)
	*/
	if( is_sticky() && is_home() )
		$classes[] = 'sticky';

	/*
	* Author class
	*/
	if ( !is_attachment() )
		$classes[] = 'author-' . sanitize_title_with_dashes( strtolower( get_the_author_login() ) );

	/*
	* Password-protected posts
	*/
	if ( $post->post_password )
		$classes[] = 'protected';

	/*
	* Add each category as a class
	*/
	if ( !is_attachment() && $post->post_type != 'page' ) :
		foreach ( (array)get_the_category( $post->ID ) as $cat ) :
			if ( empty ( $cat->slug ) )
				continue;
			$classes[] = 'category-' . $cat->slug;
		endforeach;
	endif;

	/*
	* Add each tag as a class
	*/
	if ( !is_attachment() && $post->post_type != 'page' ) :
		foreach ( ( array )get_the_tags( $post->ID ) as $tag ) :
			if ( empty( $tag->slug ) )
				continue;
			$classes[] = 'tag-' . $tag->slug;
		endforeach;
	endif;

	/*
	* User-created classes
	*/
	if ( !empty( $class ) ) :
		if ( !is_array( $class ) )
			$class = preg_split( '#\s+#', $class );
		$classes = array_merge( $classes, $class );
	endif;

	/*
	* Join all the classes into one string and echo them
	*/
	$class = join( ' ', $classes );

	echo apply_filters( 'one_entry_class', $class );
}

/**
 * Sets a class for each comment
 * Sets alt, odd/even, and author/user classes
 * Adds author, user, and reader classes
 * Needs more work because WP, by default, assigns even/odd backwards
 * 	- Odd should come first, even second
 *
 * @link http://codex.wordpress.org/Template_Tags/get_comment_author_url
 * @link http://codex.wordpress.org/Function_Reference/get_userdata
 *
 * @since 0.2
 */
function one_comment_class() {
	global $comment, $post, $current_user, $comment_first_class;
	static $comment_alt;
	$classes = array();

	/*
	* Gets default WP comment classes if they exists (WP 2.7+)
	*/
	if ( function_exists( 'get_comment_class' ) ) :
		$classes = str_replace( array( 'byuser', 'bypostauthor' ), '', get_comment_class() );
	else :
		$classes[] = get_comment_type();

		/*
		* Alt classes
		*/
		if ( ++$comment_alt % 2 )
			$classes[] = 'odd alt';
		else
			$classes[] = 'even';
	endif;

	/*
	* User classes to match user role
	* Major props to Ptah Dunbar
	* @link http://wpframework.com
	*/
	if ( $comment->user_id > 0 && $user = get_userdata( $comment->user_id ) ) :

		$classes[] = 'registered registered-' . $user->user_nicename;

		if ( $post = get_post( $post_id ) ) :
			if ( $comment->user_id === $post->post_author )
				$classes[] = 'entry-author';
		endif;

	else :
		/*
		* If not a registered user
		*/
		$classes[] = 'reader';

	endif;

	/*
	* http://microid.org
	*/
	$email = get_comment_author_email();
	$url = get_comment_author_url();
	if ( !empty( $email ) && !empty( $url ) ) :
		$microid = 'microid-mailto+http:sha1:' . sha1( sha1( 'mailto:'.$email ) . sha1( $url ) );
		$classes[] = $microid;
	endif;

	$class = join( ' ', $classes );

	echo apply_filters( 'one_comment_class', $class );
}

/**
 * Dynamic body class
 * Provides classes for the <body> element depending on page
 *
 * Mostly relies on conditional tags but several other functions are key
 * @link http://codex.wordpress.org/Conditional_Tags
 * @link http://codex.wordpress.org/Template_Tags/get_the_category
 * @link http://codex.wordpress.org/Template_Tags/get_the_tags
 * @link http://codex.wordpress.org/Function_Reference/get_userdata
 * @link http://codex.wordpress.org/Function_Reference/get_post_mime_type
 * @link http://codex.wordpress.org/Function_Reference/get_post_meta
 *
 * @since 0.1
 * @param string|array $class additional classes for more control
 * @return string
 */
function one_body_class( $class = '' ) {
	global $wp_query, $one_settings;

	$classes = array();

	/*
	* Text direction (which direction does the text flow)
	*/
	if ( 'rtl' == get_bloginfo( 'text_direction' ) )
		$classes[] = 'rtl';
	else
		$classes[] = 'ltr';

	/*
	* Is the current user logged in
	*/
	if ( is_user_logged_in() )
		$classes[] = 'logged-in';
	else
		$classes[] = 'not-logged-in';

	/*
	* Basic classes generated by is_* functions
	*/
	if ( is_front_page() )
		$classes[] = 'home front-page';
	if ( is_home() )
		$classes[] = 'blog';
	if ( is_archive() )
		$classes[] = 'archive';
	if ( is_date() )
		$classes[] = 'date';
	if ( is_category() )
		$classes[] = 'category';
	if ( is_author() )
		$classes[] = 'author';
	if ( is_tag() )
		$classes[] = 'tag';
	if ( is_attachment() )
		$classes[] = 'attachment';
	if ( is_404() )
		$classes[] = 'error-404';
	if ( is_paged() )
		$classes[] = 'paged';
	if ( is_preview() && is_single() )
		$classes[] = 'preview preview-single';
	if ( is_preview() && is_page() )
		$classes[] = 'preview preview-page';
	if ( is_comments_popup() )
		$classes[] = 'comments-popup';

	/*
	* Attachments
	*/
	if ( is_attachment() ) :
		$mime_type = get_post_mime_type();
		$mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
		$classes[] = 'attachment-' . $wp_query->post->ID . ' attachment-' . str_replace( $mime_prefix, "", "$mime_type" );
		if ( wp_attachment_is_image( $wp_query->post->ID ) ) :
			$classes[] = 'attachment-image';
		endif;

	/*
	* Single posts
	*/
	elseif ( is_single() ) :
		$classes[] = 'single single-' . $wp_query->post->ID;
		if ( is_sticky( $wp_query->post->ID ) ) :
			$classes[] = 'single-sticky';
		endif;

		if ( $categories = get_the_category() ) :
			foreach ( $categories as $cat ) :
				$classes[] = 'single-category-' . $cat->slug;
			endforeach;
		endif;

		$wp_query->in_the_loop = true;
		if ( $tags = get_the_tags() ) :
			foreach ( $tags as $tag ) :
				$classes[] = 'single-tag-' . $tag->slug;
			endforeach;
		endif;
		$wp_query->in_the_loop = false;

		$curauth = get_userdata( $wp_query->post->post_author );
		$classes[] = 'single-author-' . sanitize_title_with_dashes( strtolower( $curauth->user_login ) );

	/*
	* Pages
	*/
	elseif ( is_page() ) :
		$classes[] = 'page page-' . $wp_query->post->ID;

		if ( is_page_template() ) :
			$classes[] = 'page-template page-template-' . str_replace( '.php', '', get_post_meta( $wp_query->post->ID, '_wp_page_template', true ) );
		endif;

		$curauth = get_userdata( $wp_query->post->post_author );
		$classes[] = 'page-author-' . sanitize_title_with_dashes( strtolower( $curauth->user_login ) );

	/*
	* Archives (author, category, date, tag)
	*/
	elseif ( is_archive() ) :

		if ( is_author() ) :
			$author = $wp_query->get_queried_object();
			$classes[] = 'author-' . $author->user_nicename;
		elseif ( is_category() ) :
			$category = $wp_query->get_queried_object();
			$classes[] = 'category-' . $category->slug;
		elseif ( is_tag() ) :
			$tag = $wp_query->get_queried_object();
			$classes[] = 'tag-' . $tag->slug;
		elseif ( is_time() ) :
			$classes[] = 'time';
		elseif ( is_day() ) :
			$classes[] = 'day';
		elseif ( get_query_var( 'w' ) ) :
			$classes[] = 'week';
		elseif ( is_month() ) :
			$classes[] = 'month';
		elseif ( is_year() ) :
			$classes[] = 'year';
		endif;

	/*
	* Search results
	*/
	elseif ( is_search() ) :
		if ( have_posts() ) :
			$classes[] = 'search search-results';
		else :
			$classes[] = 'search search-no-results';
		endif;

	endif;

	/*
	* Paged views
	*/
	$page = $wp_query->get( 'page' );
	
	if ( !$page || $page < 2 )
		$page = $wp_query->get( 'paged' );

	if ( $page && $page > 1 ) :
		$classes[] = 'paged-' . $page;

		if ( is_front_page() )
			$classes[] = 'home-paged home-paged-' . $page . ' front-page-paged front-page-paged-' . $page;
		if ( is_home() )
			$classes[] = 'blog-paged blog-paged-' . $page;

		if ( is_single() )
			$classes[] = 'single-paged single-paged-' . $page;
		elseif ( is_page() )
			$classes[] = 'page-paged page-paged-' . $page;
		elseif ( is_category() )
			$classes[] = 'category-paged category-paged-' . $page;
		elseif ( is_tag() )
			$classes[] = 'tag-paged tag-paged-' . $page;
		elseif ( is_date() )
			$classes[] = 'date-paged date-paged-' . $page;
		elseif ( is_author() )
			$classes[] = 'author-paged author-paged-' . $page;
		elseif ( is_search() )
			$classes[] = 'search-paged search-paged-' . $page;
	endif;

	/*
	* Browser and OS detection
	* Major props to Ptah Dunbar (http://wpframework.com)
	*/
	$browser = $_SERVER[ 'HTTP_USER_AGENT' ];

	/*
	* OS detection
	*/
	if ( preg_match( "/Windows/", $browser ) )
		$classes[] = 'windows';
	elseif ( preg_match( "/Mac/", $browser ) )
		$classes[] = 'mac';
	elseif ( preg_match( "/Linux/", $browser ) )
		$classes[] = 'linux';
	else
		$classes[] = 'unknown-os';

	/*
	* Firefox
	*/
	if ( preg_match( "/Firefox/", $browser ) && preg_match( "/Gecko/", $browser ) ) :
		preg_match( "/Firefox\/(\d)/si", $browser, $matches );
		$classes[] = 'firefox firefox-' . str_replace( ".", "-", $matches[1] );

	/*
	* Internet Explorer
	*/
	elseif ( preg_match( "/MSIE/", $browser ) ) :
		$classes[] = 'msie';
		if ( preg_match( "/MSIE 6.0/", $browser ) ) :
			$classes[] = 'ie6';
		elseif ( preg_match( "/MSIE 7.0/", $browser ) ) :
			$classs[] = 'ie7';
		elseif ( preg_match( "/MSIE 8.0/", $browser ) ) :
			$classes[] = 'ie8';
		endif;

	/*
	* Opera
	*/
	elseif ( preg_match( "/Opera/", $browser ) ) :
		preg_match( "/Opera\/(\d.\d)/si", $browser, $matches );
		$classes[] = 'opera opera-' . str_replace( ".", "-", $matches[1] );

	/*
	* Safari
	*/
	elseif ( preg_match( "/Safari/", $browser ) ) :
		preg_match( "/Version\/(\d.\d)/si", $browser, $matches );
		$classes[] = 'safari safari-' . str_replace( ".", "-", $matches[1] );

	/*
	* Chrome
	*/
	elseif ( preg_match( "/Chrome/", $browser ) ) :
		preg_match( "/Chrome\/(\d.\d)/si", $browser, $matches );
		$classes[] = 'chrome chrome-' . str_replace( ".", "-", $matches[1] );

	/*
	* Unknown browser
	*/
	else :
		$classes[] = 'unknown-browser';

	endif;

	if ( !empty( $class ) ) :
		if ( !is_array( $class ) )
			$class = preg_split( '#\s+#', $class );
		$classes = array_merge( $classes, $class );
	endif;

// Join all the classes into one string
	$class = join( ' ', $classes );

// Return the body class
	echo apply_filters( 'one_body_class', $class );
}

?>